// Copyright (c) 2006-2007 Frank Laub
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
//    derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using NUnit.Framework;
using OpenSSL.Crypto;
using OpenSSL.Core;

namespace UnitTests
{
	[TestFixture]
	public class TestRSA : TestBase
	{
		const string rnd_seed = "string to make the random number generator think it has entropy";

		readonly byte[] ptext_ex = {
			0x54, 0x85, 0x9b, 0x34, 0x2c, 0x49, 0xea, 0x2a
		};

		byte[] Key1(RSA key)
		{
			byte[] n = {
				0x00, 0xAA, 0x36, 0xAB, 0xCE, 0x88, 0xAC, 0xFD, 0xFF, 0x55, 0x52, 0x3C, 0x7F, 0xC4, 0x52, 0x3F,
				0x90, 0xEF, 0xA0, 0x0D, 0xF3, 0x77, 0x4A, 0x25, 0x9F, 0x2E, 0x62, 0xB4, 0xC5, 0xD9, 0x9C, 0xB5,
				0xAD, 0xB3, 0x00, 0xA0, 0x28, 0x5E, 0x53, 0x01, 0x93, 0x0E, 0x0C, 0x70, 0xFB, 0x68, 0x76, 0x93,
				0x9C, 0xE6, 0x16, 0xCE, 0x62, 0x4A, 0x11, 0xE0, 0x08, 0x6D, 0x34, 0x1E, 0xBC, 0xAC, 0xA0, 0xA1,
				0xF5
			};

			byte[] e = { 0x11 };

			byte[] d = {
				0x0A, 0x03, 0x37, 0x48, 0x62, 0x64, 0x87, 0x69, 0x5F, 0x5F, 0x30, 0xBC, 0x38, 0xB9, 0x8B, 0x44,
				0xC2, 0xCD, 0x2D, 0xFF, 0x43, 0x40, 0x98, 0xCD, 0x20, 0xD8, 0xA1, 0x38, 0xD0, 0x90, 0xBF, 0x64,
				0x79, 0x7C, 0x3F, 0xA7, 0xA2, 0xCD, 0xCB, 0x3C, 0xD1, 0xE0, 0xBD, 0xBA, 0x26, 0x54, 0xB4, 0xF9,
				0xDF, 0x8E, 0x8A, 0xE5, 0x9D, 0x73, 0x3D, 0x9F, 0x33, 0xB3, 0x01, 0x62, 0x4A, 0xFD, 0x1D, 0x51,
			};

			byte[] p = {
				0x00, 0xD8, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92, 0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5,
				0xCF, 0xCE, 0x33, 0x52, 0x52, 0x4D, 0x04, 0x16, 0xA5, 0xA4, 0x41, 0xE7, 0x00, 0xAF, 0x46, 0x12,
				0x0D
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5A, 0x0F, 0x20, 0x35, 0x02, 0x8B, 0x9D,
				0x89
			};

			byte[] dmp1 = {
				0x59, 0x0B, 0x95, 0x72, 0xA2, 0xC2, 0xA9, 0xC4, 0x06, 0x05, 0x9D, 0xC2, 0xAB, 0x2F, 0x1D, 0xAF,
				0xEB, 0x7E, 0x8B, 0x4F, 0x10, 0xA7, 0x54, 0x9E, 0x8E, 0xED, 0xF5, 0xB4, 0xFC, 0xE0, 0x9E, 0x05,
			};

			byte[] dmq1 = {
				0x00, 0x8E, 0x3C, 0x05, 0x21, 0xFE, 0x15, 0xE0, 0xEA, 0x06, 0xA3, 0x6F, 0xF0, 0xF1, 0x0C, 0x99,
				0x52, 0xC3, 0x5B, 0x7A, 0x75, 0x14, 0xFD, 0x32, 0x38, 0xB8, 0x0A, 0xAD, 0x52, 0x98, 0x62, 0x8D,
				0x51
			};

			byte[] iqmp = {
				0x36, 0x3F, 0xF7, 0x18, 0x9D, 0xA8, 0xE9, 0x0B, 0x1D, 0x34, 0x1F, 0x71, 0xD0, 0x9B, 0x76, 0xA8,
				0xA9, 0x43, 0xE1, 0x1D, 0x10, 0xB2, 0x4D, 0x24, 0x9F, 0x2D, 0xEA, 0xFE, 0xF8, 0x0C, 0x18, 0x26,
			};

			byte[] ctext_ex = {
				0x1b, 0x8f, 0x05, 0xf9, 0xca, 0x1a, 0x79, 0x52, 0x6e, 0x53, 0xf3, 0xcc, 0x51, 0x4f, 0xdb, 0x89,
				0x2b, 0xfb, 0x91, 0x93, 0x23, 0x1e, 0x78, 0xb9, 0x92, 0xe6, 0x8d, 0x50, 0xa4, 0x80, 0xcb, 0x52,
				0x33, 0x89, 0x5c, 0x74, 0x95, 0x8d, 0x5d, 0x02, 0xab, 0x8c, 0x0f, 0xd0, 0x40, 0xeb, 0x58, 0x44,
				0xb0, 0x05, 0xc3, 0x9e, 0xd8, 0x27, 0x4a, 0x9d, 0xbf, 0xa8, 0x06, 0x71, 0x40, 0x94, 0x39, 0xd2,
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}

		byte[] Key2(RSA key)
		{
			byte[] n = {
				0x00, 0xA3, 0x07, 0x9A, 0x90, 0xDF, 0x0D, 0xFD, 0x72, 0xAC, 0x09, 0x0C, 0xCC, 0x2A, 0x78, 0xB8,
				0x74, 0x13, 0x13, 0x3E, 0x40, 0x75, 0x9C, 0x98, 0xFA, 0xF8, 0x20, 0x4F, 0x35, 0x8A, 0x0B, 0x26,
				0x3C, 0x67, 0x70, 0xE7, 0x83, 0xA9, 0x3B, 0x69, 0x71, 0xB7, 0x37, 0x79, 0xD2, 0x71, 0x7B, 0xE8,
				0x34, 0x77, 0xCF,
			};

			byte[] e = { 0x03 };

			byte[] d = {
				0x6C, 0xAF, 0xBC, 0x60, 0x94, 0xB3, 0xFE, 0x4C, 0x72, 0xB0, 0xB3, 0x32, 0xC6, 0xFB, 0x25, 0xA2,
				0xB7, 0x62, 0x29, 0x80, 0x4E, 0x68, 0x65, 0xFC, 0xA4, 0x5A, 0x74, 0xDF, 0x0F, 0x8F, 0xB8, 0x41,
				0x3B, 0x52, 0xC0, 0xD0, 0xE5, 0x3D, 0x9B, 0x59, 0x0F, 0xF1, 0x9B, 0xE7, 0x9F, 0x49, 0xDD, 0x21,
				0xE5, 0xEB
			};

			byte[] p = {
				0x00, 0xCF, 0x20, 0x35, 0x02, 0x8B, 0x9D, 0x86, 0x98, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92,
				0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5, 0xCF, 0xCE, 0x91
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5F
			};

			byte[] dmp1 = {
				0x00, 0x8A, 0x15, 0x78, 0xAC, 0x5D, 0x13, 0xAF, 0x10, 0x2B, 0x22, 0xB9, 0x99, 0xCD, 0x74, 0x61,
				0xF1, 0x5E, 0x6D, 0x22, 0xCC, 0x03, 0x23, 0xDF, 0xDF, 0x0B
			};

			byte[] dmq1 =
			{
				0x00, 0x86, 0x55, 0x21, 0x4A, 0xC5, 0x4D, 0x8D, 0x4E, 0xCD, 0x61, 0x77, 0xF1, 0xC7, 0x36, 0x90,
				0xCE, 0x2A, 0x48, 0x2C, 0x8B, 0x05, 0x99, 0xCB, 0xE0, 0x3F
			};

			byte[] iqmp = {
				0x00, 0x83, 0xEF, 0xEF, 0xB8, 0xA9, 0xA4, 0x0D, 0x1D, 0xB6, 0xED, 0x98, 0xAD, 0x84, 0xED, 0x13,
				0x35, 0xDC, 0xC1, 0x08, 0xF3, 0x22, 0xD0, 0x57, 0xCF, 0x8D
			};

			byte[] ctext_ex = {
				0x14, 0xbd, 0xdd, 0x28, 0xc9, 0x83, 0x35, 0x19, 0x23, 0x80, 0xe8, 0xe5, 0x49, 0xb1, 0x58, 0x2a,
				0x8b, 0x40, 0xb4, 0x48, 0x6d, 0x03, 0xa6, 0xa5, 0x31, 0x1f, 0x1f, 0xd5, 0xf0, 0xa1, 0x80, 0xe4,
				0x17, 0x53, 0x03, 0x29, 0xa9, 0x34, 0x90, 0x74, 0xb1, 0x52, 0x13, 0x54, 0x29, 0x08, 0x24, 0x52,
				0x62, 0x51
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}

		byte[] Key3(RSA key)
		{
			byte[] n = {
				0x00, 0xBB, 0xF8, 0x2F, 0x09, 0x06, 0x82, 0xCE, 0x9C, 0x23, 0x38, 0xAC, 0x2B, 0x9D, 0xA8, 0x71,
				0xF7, 0x36, 0x8D, 0x07, 0xEE, 0xD4, 0x10, 0x43, 0xA4, 0x40, 0xD6, 0xB6, 0xF0, 0x74, 0x54, 0xF5,
				0x1F, 0xB8, 0xDF, 0xBA, 0xAF, 0x03, 0x5C, 0x02, 0xAB, 0x61, 0xEA, 0x48, 0xCE, 0xEB, 0x6F, 0xCD,
				0x48, 0x76, 0xED, 0x52, 0x0D, 0x60, 0xE1, 0xEC, 0x46, 0x19, 0x71, 0x9D, 0x8A, 0x5B, 0x8B, 0x80,
				0x7F, 0xAF, 0xB8, 0xE0, 0xA3, 0xDF, 0xC7, 0x37, 0x72, 0x3E, 0xE6, 0xB4, 0xB7, 0xD9, 0x3A, 0x25,
				0x84, 0xEE, 0x6A, 0x64, 0x9D, 0x06, 0x09, 0x53, 0x74, 0x88, 0x34, 0xB2, 0x45, 0x45, 0x98, 0x39,
				0x4E, 0xE0, 0xAA, 0xB1, 0x2D, 0x7B, 0x61, 0xA5, 0x1F, 0x52, 0x7A, 0x9A, 0x41, 0xF6, 0xC1, 0x68,
				0x7F, 0xE2, 0x53, 0x72, 0x98, 0xCA, 0x2A, 0x8F, 0x59, 0x46, 0xF8, 0xE5, 0xFD, 0x09, 0x1D, 0xBD,
				0xCB,
			};

			byte[] e = { 0x11 };

			byte[] d = {
				0x00, 0xA5, 0xDA, 0xFC, 0x53, 0x41, 0xFA, 0xF2, 0x89, 0xC4, 0xB9, 0x88, 0xDB, 0x30, 0xC1, 0xCD,
				0xF8, 0x3F, 0x31, 0x25, 0x1E, 0x06, 0x68, 0xB4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41,
				0xB2, 0x94, 0x10, 0xB3, 0xC7, 0x99, 0x8D, 0x6B, 0xC4, 0x65, 0x74, 0x5E, 0x5C, 0x39, 0x26, 0x69,
				0xD6, 0x87, 0x0D, 0xA2, 0xC0, 0x82, 0xA9, 0x39, 0xE3, 0x7F, 0xDC, 0xB8, 0x2E, 0xC9, 0x3E, 0xDA,
				0xC9, 0x7F, 0xF3, 0xAD, 0x59, 0x50, 0xAC, 0xCF, 0xBC, 0x11, 0x1C, 0x76, 0xF1, 0xA9, 0x52, 0x94,
				0x44, 0xE5, 0x6A, 0xAF, 0x68, 0xC5, 0x6C, 0x09, 0x2C, 0xD3, 0x8D, 0xC3, 0xBE, 0xF5, 0xD2, 0x0A,
				0x93, 0x99, 0x26, 0xED, 0x4F, 0x74, 0xA1, 0x3E, 0xDD, 0xFB, 0xE1, 0xA1, 0xCE, 0xCC, 0x48, 0x94,
				0xAF, 0x94, 0x28, 0xC2, 0xB7, 0xB8, 0x88, 0x3F, 0xE4, 0x46, 0x3A, 0x4B, 0xC8, 0x5B, 0x1C, 0xB3,
				0xC1,
			};

			byte[] p = {
				0x00, 0xEE, 0xCF, 0xAE, 0x81, 0xB1, 0xB9, 0xB3, 0xC9, 0x08, 0x81, 0x0B, 0x10, 0xA1, 0xB5, 0x60,
				0x01, 0x99, 0xEB, 0x9F, 0x44, 0xAE, 0xF4, 0xFD, 0xA4, 0x93, 0xB8, 0x1A, 0x9E, 0x3D, 0x84, 0xF6,
				0x32, 0x12, 0x4E, 0xF0, 0x23, 0x6E, 0x5D, 0x1E, 0x3B, 0x7E, 0x28, 0xFA, 0xE7, 0xAA, 0x04, 0x0A,
				0x2D, 0x5B, 0x25, 0x21, 0x76, 0x45, 0x9D, 0x1F, 0x39, 0x75, 0x41, 0xBA, 0x2A, 0x58, 0xFB, 0x65,
				0x99,
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5A, 0x0F, 0x20, 0x35, 0x02, 0x8B, 0x9D,
				0x86, 0x98, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92, 0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5,
				0xCF, 0xCE, 0x33, 0x52, 0x52, 0x4D, 0x04, 0x16, 0xA5, 0xA4, 0x41, 0xE7, 0x00, 0xAF, 0x46, 0x15,
				0x03,
			};

			byte[] dmp1 = {
				0x54, 0x49, 0x4C, 0xA6, 0x3E, 0xBA, 0x03, 0x37, 0xE4, 0xE2, 0x40, 0x23, 0xFC, 0xD6, 0x9A, 0x5A,
				0xEB, 0x07, 0xDD, 0xDC, 0x01, 0x83, 0xA4, 0xD0, 0xAC, 0x9B, 0x54, 0xB0, 0x51, 0xF2, 0xB1, 0x3E,
				0xD9, 0x49, 0x09, 0x75, 0xEA, 0xB7, 0x74, 0x14, 0xFF, 0x59, 0xC1, 0xF7, 0x69, 0x2E, 0x9A, 0x2E,
				0x20, 0x2B, 0x38, 0xFC, 0x91, 0x0A, 0x47, 0x41, 0x74, 0xAD, 0xC9, 0x3C, 0x1F, 0x67, 0xC9, 0x81,
			};

			byte[] dmq1 = {
				0x47, 0x1E, 0x02, 0x90, 0xFF, 0x0A, 0xF0, 0x75, 0x03, 0x51, 0xB7, 0xF8, 0x78, 0x86, 0x4C, 0xA9,
				0x61, 0xAD, 0xBD, 0x3A, 0x8A, 0x7E, 0x99, 0x1C, 0x5C, 0x05, 0x56, 0xA9, 0x4C, 0x31, 0x46, 0xA7,
				0xF9, 0x80, 0x3F, 0x8F, 0x6F, 0x8A, 0xE3, 0x42, 0xE9, 0x31, 0xFD, 0x8A, 0xE4, 0x7A, 0x22, 0x0D,
				0x1B, 0x99, 0xA4, 0x95, 0x84, 0x98, 0x07, 0xFE, 0x39, 0xF9, 0x24, 0x5A, 0x98, 0x36, 0xDA, 0x3D,
			};

			byte[] iqmp = {
				0x00, 0xB0, 0x6C, 0x4F, 0xDA, 0xBB, 0x63, 0x01, 0x19, 0x8D, 0x26, 0x5B, 0xDB, 0xAE, 0x94, 0x23,
				0xB3, 0x80, 0xF2, 0x71, 0xF7, 0x34, 0x53, 0x88, 0x50, 0x93, 0x07, 0x7F, 0xCD, 0x39, 0xE2, 0x11,
				0x9F, 0xC9, 0x86, 0x32, 0x15, 0x4F, 0x58, 0x83, 0xB1, 0x67, 0xA9, 0x67, 0xBF, 0x40, 0x2B, 0x4E,
				0x9E, 0x2E, 0x0F, 0x96, 0x56, 0xE6, 0x98, 0xEA, 0x36, 0x66, 0xED, 0xFB, 0x25, 0x79, 0x80, 0x39,
				0xF7,
			};

			byte[] ctext_ex = {
				0xb8, 0x24, 0x6b, 0x56, 0xa6, 0xed, 0x58, 0x81, 0xae, 0xb5, 0x85, 0xd9, 0xa2, 0x5b, 0x2a, 0xd7,
				0x90, 0xc4, 0x17, 0xe0, 0x80, 0x68, 0x1b, 0xf1, 0xac, 0x2b, 0xc3, 0xde, 0xb6, 0x9d, 0x8b, 0xce,
				0xf0, 0xc4, 0x36, 0x6f, 0xec, 0x40, 0x0a, 0xf0, 0x52, 0xa7, 0x2e, 0x9b, 0x0e, 0xff, 0xb5, 0xb3,
				0xf2, 0xf1, 0x92, 0xdb, 0xea, 0xca, 0x03, 0xc1, 0x27, 0x40, 0x05, 0x71, 0x13, 0xbf, 0x1f, 0x06,
				0x69, 0xac, 0x22, 0xe9, 0xf3, 0xa7, 0x85, 0x2e, 0x3c, 0x15, 0xd9, 0x13, 0xca, 0xb0, 0xb8, 0x86,
				0x3a, 0x95, 0xc9, 0x92, 0x94, 0xce, 0x86, 0x74, 0x21, 0x49, 0x54, 0x61, 0x03, 0x46, 0xf4, 0xd4,
				0x74, 0xb2, 0x6f, 0x7c, 0x48, 0xb4, 0x2e, 0xe6, 0x8e, 0x1f, 0x57, 0x2a, 0x1f, 0xc4, 0x02, 0x6a,
				0xc4, 0x56, 0xb4, 0xf5, 0x9f, 0x7b, 0x62, 0x1e, 0xa1, 0xb9, 0xd8, 0x8f, 0x64, 0x20, 0x2f, 0xb1,
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}

		private void SetKey(RSA key, byte[] n, byte[] e, byte[] d, byte[] p, byte[] q, byte[] dmp1, byte[] dmq1, byte[] iqmp)
		{
			using (BigNumber bn = BigNumber.FromArray(n))
				key.PublicModulus = bn;
			using (BigNumber bn = BigNumber.FromArray(e))
				key.PublicExponent = bn;
			using (BigNumber bn = BigNumber.FromArray(d))
				key.PrivateExponent = bn;
			using (BigNumber bn = BigNumber.FromArray(p))
				key.SecretPrimeFactorP = bn;
			using (BigNumber bn = BigNumber.FromArray(q))
				key.SecretPrimeFactorQ = bn;
			using (BigNumber bn = BigNumber.FromArray(dmp1))
				key.DmodP1 = bn;
			using (BigNumber bn = BigNumber.FromArray(dmq1))
				key.DmodQ1 = bn;
			using (BigNumber bn = BigNumber.FromArray(iqmp))
				key.IQmodP = bn;
		}

		private void TestKey(int v, RSA key)
		{
			byte[] ctext_ex = null;
			switch (v % 3) {
				case 0: ctext_ex = Key1(key); break;
				case 1: ctext_ex = Key2(key); break;
				case 2: ctext_ex = Key3(key); break;
			}

			if (v / 3 >= 1)
				key.ConstantTime = false;

			byte[] ctext = key.PublicEncrypt(ptext_ex, RSA.Padding.PKCS1);
			Assert.AreEqual(ctext_ex.Length, ctext.Length);

			byte[] ptext = key.PrivateDecrypt(ctext, RSA.Padding.PKCS1);
			string str1 = BitConverter.ToString(ptext);
			string str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("PKCS #1 v1.5 encryption/decryption ok");

			ctext = key.PublicEncrypt(ptext_ex, RSA.Padding.OAEP);
			Assert.AreEqual(ctext_ex.Length, ctext.Length);

			ptext = key.PrivateDecrypt(ctext, RSA.Padding.OAEP);
			str1 = BitConverter.ToString(ptext);
			str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("OAEP test vector {0} passed!", v);

			// Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
			// Try decrypting ctext_ex
			ptext = key.PrivateDecrypt(ctext_ex, RSA.Padding.OAEP);
			str1 = BitConverter.ToString(ptext);
			str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("OAEP encryption/decryption ok!", v);
			for (int n = 0; n < ctext.Length; ++n) {
				byte saved = ctext[n];
				for (byte b = 0; b < byte.MaxValue; ++b) {
					if (b == saved)
						continue;

					ctext[n] = b;
					bool error = false;
					try {
						ptext = key.PrivateDecrypt(ctext, RSA.Padding.OAEP);
					}
					catch (Exception) {
						error = true;
					}
					Assert.IsTrue(error, "Corrupt data decrypted!");
				}
			}
		}

		[Test]
		public void TestCase()
		{
			OpenSSL.Core.Random.Seed(rnd_seed);

			for (int v = 0; v < 6; v++) {
				using (RSA key = new RSA()) {
					TestKey(v, key);
				}
			}
		}
	}
}

